﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace LongSpeed.WebSite.Behaviors
{
    [Serializable]
    public class WebSiteConfig
    {
        public string Tilte { get; set; }
        public string Description { get; set; }
        public string Address { get; set; }
        public string ContactPhone { get; set; }
        public string Copyright { get; set; }
        public string KeyWords { get; set; }


        public void Save(System.Web.UI.Page page)
        {
            string path = page.Server.MapPath("~/App_Data/SiteConfig.dat");
            if (Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, this);
            fs.Flush();
            fs.Close();
            fs.Dispose();
        }

        public WebSiteConfig Init(HttpServerUtility serverUtility)
        {
            string path = serverUtility.MapPath("~/App_Data/SiteConfig.dat");

            if (!File.Exists(path))
            {
                return null;
            }

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            BinaryFormatter bf = new BinaryFormatter();
            var config = bf.Deserialize(fs) as WebSiteConfig;


            fs.Flush();
            fs.Close();
            fs.Dispose();     
            
            return config;

        }
    }
}